//
//  YMHomeVC.swift
//  NewVersionDemo
//
//  Created by zeng on 2018/3/12.
//  Copyright © 2018年 youmy. All rights reserved.
//

import UIKit

import MJRefresh

fileprivate let kCellId = "kCellId"
fileprivate let kNormalCellId = "kNormalCellId"

fileprivate let kCellReusableId = "kCellReusableId"
fileprivate let kNormalReusableId = "kNormalReusableId"
fileprivate let kFooterId = "kFooterId"

fileprivate let kItemMargin:CGFloat = 1
fileprivate let kItemW:CGFloat = (ym_screen_width - 1 * kItemMargin) / 2

class YMHomeVC: UIViewController {
    
    var homeReusableView:YMHomeReusableView?
    
    lazy var actionView = YMHomActionView.nibView()
    
    lazy var collectionView:UICollectionView = {
        
        let kItemH:CGFloat = 195
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = kItemMargin
        layout.minimumInteritemSpacing = kItemMargin
        layout.sectionInset = UIEdgeInsetsMake(kItemMargin, 0, kItemMargin, 0)
                
        let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.backgroundColor = ym_bg_color
        collectionView.delegate = self
        collectionView.dataSource = self
        
        if #available(iOS 11.0, *) {
            collectionView.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
        }
        
        collectionView.register(UINib.init(nibName: "YMHomeBrandCell", bundle: nil), forCellWithReuseIdentifier: kCellId)
        collectionView.register(UINib.init(nibName: "YMHomeNormalCell", bundle: nil), forCellWithReuseIdentifier: kNormalCellId)
        collectionView.register(UINib.init(nibName: "YMHomeReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kCellReusableId)
        collectionView.register(UINib.init(nibName: "YMHomeSubReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kNormalReusableId)
        collectionView.register(UINib.init(nibName: "YMNoMoreDataView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: kFooterId)
        
        return collectionView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(collectionView)
        
        self.view.addSubview(actionView)
        
        actionView.didClick = {[weak self] (isLogin) in
            if isLogin == false {
                self?.actionView.removeFromSuperview()
            }else{
                print("登录")
            }
        }
        
        collectionView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            DispatchQueue.main.asyncAfter(deadline: .now() + 3, execute: {
                self.collectionView.mj_header.endRefreshing()
            })
        })
    }
}

extension YMHomeVC:UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if indexPath.section == 0 {
            let reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kCellReusableId, for: indexPath) as! YMHomeReusableView
            reusableView.recommansModels = [["icon":"icon-每日上新","title":"每日上新"],
                                            ["icon":"icon-精心推荐","title":"精心推荐"],
                                            ["icon":"icon-信用租机","title":"信用租机"]]
            
            return reusableView
        }else{
            if kind == UICollectionElementKindSectionHeader {
                let reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kNormalReusableId, for: indexPath) as! YMHomeSubReusableView
                return reusableView
            }else{
                let reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: kFooterId, for: indexPath) as! YMNoMoreDataView
                reusableView.contentLbl.text = "到底了，没有更多了哦~"
                return reusableView
            }
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        if section == 0 {
            return CGSize(width: ym_screen_width, height: 633)
        }else{
            return CGSize(width: ym_screen_width, height: 173)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        if section == 0 {
            return CGSize(width: ym_screen_width, height: 0)
        }else{
            return CGSize(width: ym_screen_width, height: 50)
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 0 {
            return CGSize(width: kItemW, height: 195)
        }else{
            return CGSize(width: kItemW, height: 200)
        }
    }
}

extension YMHomeVC:UICollectionViewDataSource,UICollectionViewDelegate{
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 1 {
            return 6
        }
        return 4
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        if indexPath.section == 0 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCellId, for: indexPath) as! YMHomeBrandCell
            return cell
        }else{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kNormalCellId, for: indexPath) as! YMHomeNormalCell
            return cell
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("点击了：\(indexPath.section == 0 ? "品牌专场" : "信用免押"),\(indexPath.item)")
    }
}

